<template>
  <div class="parent d-flex jc-start ai-center">
    <div class="left pr-10 mr-50">
      <el-form label-width="100px">
        <div class="d-flex flex-column jc-center ai-center mb-100">
          <UploadImg @updateFile="handleUpload" />
          <div class="mt-20">{{ $t('renlianbidui.uploadFaceToSeach') }}</div>
        </div>
        <el-form-item  :label="$t('renlianbidui.threshold') + ':'">
          <ShowTooltipSlider
            class="slider"
            :min="0"
            :max="100"
            v-model="formData.threshold"
            @change="handleCompare"
          ></ShowTooltipSlider>
        </el-form-item>
        <el-form-item  :label="$t('renlianbidui.lib_name') + ':'"  prop="lib_ids">
          <el-select v-model="formData.lib_ids" @change="handleCompare" multiple collapse-tags>
            <el-option v-for="item in libList" :key="item.id" :label="item.label" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="TOP-N:" prop="topk">
          <el-select v-model="formData.topk" @change="handleCompare">
            <el-option
              v-for="item in topkList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="divider"></div>
    <div class="right flex-1 d-flex jc-start ai-start">
      <StaffCard
        class="mr-30 mb-30"
        v-for="(item,index) in topnList"
        :cardData="item"
        :key="index"
      />
    </div>
  </div>
</template>

<script>
import UploadImg from "@/components/UploadImg";
import { compare } from "./service";
import { getLibs } from "../service";
import { MOSHENGREN_ID } from "@/utils/CONSTANT";
import StaffCard from "../StaffCard";
import ShowTooltipSlider from "../ShowTooltipSlider";
export default {
  components: { UploadImg, StaffCard, ShowTooltipSlider },
  data() {
    return {
      formData: {
        threshold: 70,
        topk: 10
      },
      libList: [{ label: this.$t('route.moshengrenku'), id: MOSHENGREN_ID }],
      topkList: [
        { label: "10", value: 10 },
        { label: "20", value: 20 },
        { label: "30", value: 30 },
        { label: "40", value: 40 },
        { label: "50", value: 50 }
      ],
      topnList: []
    };
  },
  mounted() {
    this.getLibs();
  },
  methods: {
    async handleCompare() {
      if (!this.formData.img) return;
      const params = {
        ...this.formData,
        threshold:100-this.formData.threshold,
        lib_ids: this.formData.lib_ids.join(",")
      };
      const res = await compare(params);
      this.$set(this,'topnList',res.data.topn)
    },
    async getLibs() {
      const res = await getLibs();
      res.data = res.data.map(item => {
        return { label: item.lib_name, id: item.lib_id };
      });
      this.libList = this.libList.concat(res.data);
      this.$set(
        this.formData,
        "lib_ids",
        this.libList.map(item => item.id)
      );
    },
    handleUpload(file) {
      this.formData.img = file;
      this.handleCompare();
    },
  }
};
</script>

<style lang="less" scoped>
.parent {
  padding-left: 50px;
  padding-right: 50px;
  .right {
    flex-wrap:wrap;
    padding-left:50px;
  }
  .divider {
    background-color: rgba(217, 222, 229, 1);
    width: 2px;
    height: 845px;
    margin-left: 80px;
  }
}
</style>